
from Base import *

class CTimeElement(CItem):
    _gName = "Time Element"
    _gNameDate = "Date"
    _gNameTime = "Time"

    _gEleLength = 4
    def __init__(self, name=_gName):
        super(CTimeElement, self).__init__(name)

    def InitSubItem(self):
        name = self._gNameDate
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameTime
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

    def Parse(self, dataStr):
        self.InitSubItem()

        self._startIndex = dataStr.GetIndex()

        byte1 = dataStr.GetByte()
        byte2 = dataStr.GetByte()
        byte3 = dataStr.GetByte()
        byte4 = dataStr.GetByte()

        year = (byte1 >> 2) + 1990
        month = ((byte1 & 0x03) << 2) | ((byte2 & 0xC0) >> 6)
        day = (byte2 & 0x3E) >> 1
        hour = ((byte2 & 0x01) << 4) | ((byte3 & 0xF0) >> 4)
        min = ((byte3 & 0x0F) << 2) | ((byte4 & 0xC0) >> 6)
        sec = byte4 & 0x3F

        item = self._subList[self._gNameDate]
        item._rawStr = "{y:02d}-{m:02d}-{d:02d}".format(y=year, m=month, d=day)

        item = self._subList[self._gNameTime]
        item._rawStr = "{h:02d}:{m:02d}:{s:02d}".format(h=hour, m=min, s=sec)

        self._rawStr = DATA_STRING_FORMAT.format(dataStr.GetString(self._startIndex, self._startIndex + self._gEleLength))

